//
//  ActivityDetailsViewController.m
//  UI_元旦豆瓣
//
//  Created by dllo on 15/12/31.
//  Copyright © 2015年 31付冬梅. All rights reserved.
//

#import "ActivityDetailsViewController.h"
#import "ActivityLabel.h"
#import "UIImageView+WebCache.h"
#import "ActivityModel.h"
#import "AfraDataBase--MovieActivity.h"
@interface ActivityDetailsViewController ()

@end

@implementation ActivityDetailsViewController
- (instancetype)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        
    }
    return self;
}
- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
//    self.view.backgroundColor = [UIColor yellowColor];
    UIScrollView *scroll = [[UIScrollView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, self.view.frame.size.height)];
    [self.view addSubview:scroll];
    scroll.contentSize = CGSizeMake(0, self.view.frame.size.height * 10);
    self.title = self.str;
    UIBarButtonItem *item1 = [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemReply target:self action:@selector(itemAction1:)];
    self.navigationItem.leftBarButtonItem = item1;
    UIBarButtonItem *item2 = [[UIBarButtonItem alloc] initWithTitle:@"收藏" style:UIBarButtonItemStylePlain target:self action:@selector(itemAction2:)];

    
    self.navigationItem.rightBarButtonItem = item2;
    
    UIView *view1 = [[UIView alloc] initWithFrame:CGRectMake(0, 0, self.view.frame.size.width, 260)];
    [scroll addSubview:view1];
    ActivityLabel *label = [[ActivityLabel alloc] initWithFrame:CGRectMake(0, 0, 350, 240)];
    [view1 addSubview:label];
    
    label.titleLabel.text = self.title;
    label.timeLabel.text = self.time;
    label.typeLabel.text = self.type;
    label.addressLabel.text = self.address;
//    label.activityImage.image = self.activityView;
    [label.activityImage sd_setImageWithURL:[NSURL URLWithString:self.activityView]];
    UILabel *label1 = [[UILabel alloc] initWithFrame:CGRectMake(20, 250, 350, 50)];
    label1.text = @"活动介绍";
    label1.font = [UIFont systemFontOfSize:25];
    [scroll addSubview:label1];
    UILabel *detailsLabel = [[UILabel alloc] init];
//    initWithFrame:CGRectMake(20, 310, 340, 8000)];
//    detailsLabel.backgroundColor = [UIColor cyanColor];
    detailsLabel.textColor = [UIColor blackColor];
    
    //    detailsTextField.contentVerticalAlignment = UIControlContentVerticalAlignmentCenter;
    [scroll addSubview:detailsLabel];
    detailsLabel.numberOfLines = 2000;
    detailsLabel.text = self.strDetails;
    CGRect rect = [detailsLabel.text boundingRectWithSize:CGSizeMake(340, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:nil  context:nil];
    [detailsLabel setFrame:CGRectMake(20, 340, 340, rect.size.height)];
    detailsLabel.textAlignment = NSTextAlignmentLeft;
    
    //顶端对齐问题
//    UILabel *dateLabel = [[UILabel alloc] init];
//    
//    NSString *dateString = _mDic[@"content"];
//    NSDictionary *art = @{NSFontAttributeName: [UIFont systemFontOfSize:17]};
//    
//    CGRect rect = [dateString boundingRectWithSize:CGSizeMake(340, 10000) options:NSStringDrawingUsesLineFragmentOrigin attributes:art context:nil];
//    [dateLabel setFrame:CGRectMake(20, 340, 340, rect.size.height)];
//    
//    dateLabel.text = dateString;
//    dateLabel.numberOfLines = 2000;
//    dateLabel.backgroundColor = [UIColor whiteColor];
//    dateLabel.textAlignment = NSTextAlignmentLeft;
    NSArray *arr = [[AfraDataBase__MovieActivity sharedInstenceDataBaseHandle] selectTable];
    
    for (ActivityModel *model in arr) {
        NSLog(@"model%@", model.title);
       
        if ([_str isEqualToString:model.title]) {
            NSLog(@"label%@", _str);
            self.navigationItem.rightBarButtonItem.title = @"已收藏";
        }
    }
    
    
    
    
}
- (void)itemAction2:(UIBarButtonItem *)item2 {
   
    
    if ([self.navigationItem.rightBarButtonItem.title isEqualToString:@"收藏"]) {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action1 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            self.navigationItem.rightBarButtonItem.title = @"已收藏";

            
        }];
        [alert addAction:action1];
        [self presentViewController:alert animated:YES completion:^{
            
        }];
        
        ActivityModel *model = [ActivityModel activityModel];
        model.title = self.title;
//        model.begin_time = self.
        //            model.end_time =
        model.address = self.address;
        
        //            model.participant_count =
        //            model.wisher_count =
        model.image = self.activityView;
        //            model.content =
        model.category_name = self.type;
        [[AfraDataBase__MovieActivity sharedInstenceDataBaseHandle] insertModel:model];
        
    } else {
        UIAlertController *alert1 = [UIAlertController alertControllerWithTitle:@"提示" message:@"该活动已经被收藏过" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action2 = [UIAlertAction actionWithTitle:@"确定" style:UIAlertActionStyleDefault handler:nil];
        [alert1 addAction:action2];
        [self presentViewController:alert1 animated:YES completion:nil];
        
    }
    
}

- (void)itemAction1:(UIBarButtonItem *)item {
    [self.navigationController popViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
